Introduction
The Events Module in xpander.ai SDK provides functionality for handling background tasks and event streaming. It integrates seamlessly with the platform for real-time task execution and monitoring.Overview
The SDK supports comprehensive event handling systems:-
Agent Event Listeners
- Set up using the
@on_taskdecorators - Handle incoming task execution requests
- Integrate with Server Sent Events (SSE) for real-time communication
- Includes strict validation to ensure the task parameter is correctly specified and returned
- Set up using the
-
Application Lifecycle Management
- Use
@on_bootdecorators for initialization tasks before event listeners start - Use
@on_shutdowndecorators for cleanup tasks during application shutdown - Support both synchronous and asynchronous functions
- Use
-
Task Event Streaming
- Monitor updates in real-time during task execution
- Track task progress, tool calls, and lifecycle events
Strict Validation
The@on_task decorator includes comprehensive validation to ensure proper usage and prevent runtime errors:
Parameter Validation
- Required
taskParameter: The decorated function must have a parameter namedtask - Task Type Validation: The
taskparameter must be an instance of theTaskclass - Return Type Validation: The function must return an instance of the
Taskclass - Test Task Validation: If provided,
test_taskmust be an instance ofLocalTaskTest
Validation Examples
Valid Function Signature
Invalid Function Signatures (Will Raise Errors)
Examples
Agent Event Listeners
Handle incoming task execution requests for agent deployment.Asynchronous Example
Using@on_task decorator to create an event handler:
Synchronous Example
Advanced Decorator Usage
The@on_task decorator supports additional parameters for advanced use cases:
Using Custom Configuration
Using Test Tasks for Local Development
Task Event Streaming
Monitor task execution in real-time.Asynchronous Example
Synchronous Example
Lifecycle Management
Manage application initialization and cleanup with lifecycle decorators:Using @on_boot for Initialization
Using @on_shutdown for Cleanup
Continue to the [Events API Reference](/API reference/events/API reference) for detailed @on_task documentation or see [Lifecycle Decorators](/API reference/events/API reference/lifecycle) for @on_boot and @on_shutdown reference.
Support
For additional help:- Full SDK Documentation
- Email: dev@xpander.ai

